package com.pawn.jwt.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;

/**
 * @program: jwt
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-08-13
 **/

public class JWTUtil {

    public static final String SING = "zhangbaoguo";

    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.HOUR,2);
        JWTCreator.Builder build = JWT.create();

        map.forEach((k,v)->{
            build.withClaim(k,v);
        });
        build.withExpiresAt(instance.getTime());
        String token = build.sign(Algorithm.HMAC256(SING));
        return token;
    }

    public static DecodedJWT verify(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
        return verify;
    }

}
